package com.qqtech.qquan.push.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qqtech.core.common.util.JsonUtil;
import com.qqtech.core.common.util.SettingCfgUtil;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.qquan.pub.enums.PubConst;
import com.qqtech.qquan.pub.enums.PubPlatformEnum;
import com.qqtech.qquan.pub.model.PubUpgradeContent;
import com.qqtech.qquan.push.enums.PushBizzEnum;
import com.qqtech.qquan.push.enums.PushConst;
import com.qqtech.qquan.push.enums.PushTypeEnum;
import com.qqtech.qquan.push.model.PushLogVo;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.Audience.Builder;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

public class PushPayloadHelper {
	/**
	 * 组装推送通知体
	 * 
	 * @param pushLogVo
	 * @return
	 */
	public static PushPayload buildAlert(PushLogVo pushLogVo) {
		if (StringUtil.isBlank(pushLogVo.getAlert())) {
			return null;
		}
		String apnsProduction = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.apnsProduction",
				"2");
		String sound = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.sound", "");
		String badge = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.badge", "1");
		IosNotification.Builder iosAlertBuilder = IosNotification.newBuilder();
		AndroidNotification.Builder androidAlertBuilder = AndroidNotification.newBuilder();
		iosAlertBuilder.setAlert(pushLogVo.getAlert());
		androidAlertBuilder.setAlert(pushLogVo.getAlert());
		if (StringUtil.isNotBlank(sound)) {
			iosAlertBuilder.setSound(sound);
		}
		if (StringUtil.isNotBlank(badge)) {
			iosAlertBuilder.setBadge(new Integer(badge));
		}
		if (StringUtil.isNotBlank(pushLogVo.getTitle())) {
			androidAlertBuilder.setTitle(pushLogVo.getTitle());
		}
		if (pushLogVo.getMapExtras() != null && pushLogVo.getMapExtras().size() > 0) {
			iosAlertBuilder.addExtras(pushLogVo.getMapExtras());
			androidAlertBuilder.addExtras(pushLogVo.getMapExtras());
		}
		Options.Builder optBuilder = Options.newBuilder();
		optBuilder.setTimeToLive(pushLogVo.getTimeToLive() == null ? 0 : pushLogVo.getTimeToLive())
				.setApnsProduction("1".equals(apnsProduction));
		if (StringUtil.isNotBlank(pushLogVo.getOverrideMsgId())) {
			optBuilder.setOverrideMsgId(new Long(pushLogVo.getOverrideMsgId()));
		}
		Notification.Builder notificationBuilder = Notification.newBuilder();
		if (pushLogVo.getPlatform() == null || PubPlatformEnum.ALL.getCode() == pushLogVo.getPlatform().intValue()) {
			notificationBuilder = notificationBuilder.addPlatformNotification(androidAlertBuilder.build())
					.addPlatformNotification(iosAlertBuilder.build());
		} else {
			if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.ANDROID.getCode() == pushLogVo.getPlatform().intValue()) {
				notificationBuilder = notificationBuilder.addPlatformNotification(androidAlertBuilder.build());
			} else if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.IOS.getCode() == pushLogVo.getPlatform().intValue()) {
				notificationBuilder = notificationBuilder.addPlatformNotification(iosAlertBuilder.build());
			}
		}
		return PushPayload.newBuilder().setPlatform(Platform.all()).setAudience(buildAudience(pushLogVo))
				.setNotification(notificationBuilder.build()).setOptions(optBuilder.build()).build();
	}

	/**
	 * 组装自定义消息体
	 * 
	 * @param pushLogVo
	 * @return
	 */
	public static PushPayload buildMessage(PushLogVo pushLogVo) {
		if (StringUtil.isBlank(pushLogVo.getMessage())) {
			return null;
		}
		String apnsProduction = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.apnsProduction",
				"2");
		Message.Builder msgBuilder = Message.newBuilder();
		msgBuilder.setMsgContent(pushLogVo.getMessage());
		if (pushLogVo.getMapExtras() != null && pushLogVo.getMapExtras().size() > 0) {
			msgBuilder.addExtras(pushLogVo.getMapExtras());
		}
		Options.Builder optBuilder = Options.newBuilder();
		optBuilder.setTimeToLive(pushLogVo.getTimeToLive() == null ? 0 : pushLogVo.getTimeToLive())
				.setApnsProduction("1".equals(apnsProduction));
		if (StringUtil.isNotBlank(pushLogVo.getOverrideMsgId())) {
			optBuilder.setOverrideMsgId(new Long(pushLogVo.getOverrideMsgId()));
		}
		Platform platform = Platform.all();
		if (pushLogVo.getPlatform() == null || PubPlatformEnum.ALL.getCode() == pushLogVo.getPlatform().intValue()) {
			platform = Platform.all();
		} else {
			if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.ANDROID.getCode() == pushLogVo.getPlatform().intValue()) {
				platform = Platform.android();
			} else if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.IOS.getCode() == pushLogVo.getPlatform().intValue()) {
				platform = Platform.ios();
			}
		}
		return PushPayload.newBuilder().setPlatform(platform).setAudience(buildAudience(pushLogVo))
				.setMessage(msgBuilder.build()).setOptions(optBuilder.build()).build();
	}

	/**
	 * 同时组装推送通知和自定义消息
	 * 
	 * @param pushLogVo
	 * @return
	 */
	public static PushPayload buildAlertNMessage(PushLogVo pushLogVo) {
		if (StringUtil.isBlank(pushLogVo.getAlert()) && StringUtil.isBlank(pushLogVo.getMessage())) {
			return null;
		}
		String apnsProduction = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.apnsProduction",
				"2");
		String sound = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.sound", "");
		String badge = SettingCfgUtil.getConfigHelper("push.properties").getValue("push.jg.badge", "1");
		PushPayload.Builder pushPayloadBuilder = PushPayload.newBuilder().setPlatform(Platform.all())
				.setAudience(buildAudience(pushLogVo));
		IosNotification.Builder iosAlertBuilder = IosNotification.newBuilder();
		AndroidNotification.Builder androidAlertBuilder = AndroidNotification.newBuilder();
		if (StringUtil.isNotBlank(pushLogVo.getAlert())) {
			iosAlertBuilder.setAlert(pushLogVo.getAlert());
			androidAlertBuilder.setAlert(pushLogVo.getAlert());
			if (StringUtil.isNotBlank(sound)) {
				iosAlertBuilder.setSound(sound);
			}
			if (StringUtil.isNotBlank(badge)) {
				iosAlertBuilder.setBadge(new Integer(badge));
			}
			if (StringUtil.isNotBlank(pushLogVo.getTitle())) {
				androidAlertBuilder.setTitle(pushLogVo.getTitle());
			}
			if (pushLogVo.getMapExtras() != null && pushLogVo.getMapExtras().size() > 0) {
				iosAlertBuilder.addExtras(pushLogVo.getMapExtras());
				androidAlertBuilder.addExtras(pushLogVo.getMapExtras());
			}
			Notification.Builder notificationBuilder = Notification.newBuilder();
			if (pushLogVo.getPlatform() == null
					|| PubPlatformEnum.ALL.getCode() == pushLogVo.getPlatform().intValue()) {
				notificationBuilder = notificationBuilder.addPlatformNotification(androidAlertBuilder.build())
						.addPlatformNotification(iosAlertBuilder.build());
			} else {
				if (pushLogVo.getPlatform() != null
						&& PubPlatformEnum.ANDROID.getCode() == pushLogVo.getPlatform().intValue()) {
					notificationBuilder = notificationBuilder.addPlatformNotification(androidAlertBuilder.build());
				} else if (pushLogVo.getPlatform() != null
						&& PubPlatformEnum.IOS.getCode() == pushLogVo.getPlatform().intValue()) {
					notificationBuilder = notificationBuilder.addPlatformNotification(iosAlertBuilder.build());
				}
			}
			pushPayloadBuilder.setNotification(notificationBuilder.build());
		}
		Message.Builder msgBuilder = Message.newBuilder();
		if (StringUtil.isNotBlank(pushLogVo.getMessage())) {
			msgBuilder.setMsgContent(pushLogVo.getMessage());
			if (pushLogVo.getMapExtras() != null && pushLogVo.getMapExtras().size() > 0) {
				msgBuilder.addExtras(pushLogVo.getMapExtras());
			}
			pushPayloadBuilder.setMessage(msgBuilder.build());
		}
		Options.Builder optBuilder = Options.newBuilder();
		optBuilder.setTimeToLive(pushLogVo.getTimeToLive() == null ? 0 : pushLogVo.getTimeToLive())
				.setApnsProduction("1".equals(apnsProduction));
		if (StringUtil.isNotBlank(pushLogVo.getOverrideMsgId())) {
			optBuilder.setOverrideMsgId(new Long(pushLogVo.getOverrideMsgId()));
		}
		Platform platform = Platform.all();
		if (pushLogVo.getPlatform() == null || PubPlatformEnum.ALL.getCode() == pushLogVo.getPlatform().intValue()) {
			platform = Platform.all();
		} else {
			if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.ANDROID.getCode() == pushLogVo.getPlatform().intValue()) {
				platform = Platform.android();
			} else if (pushLogVo.getPlatform() != null
					&& PubPlatformEnum.IOS.getCode() == pushLogVo.getPlatform().intValue()) {
				platform = Platform.ios();
			}
		}
		pushPayloadBuilder.setPlatform(platform).setOptions(optBuilder.build());
		return pushPayloadBuilder.build();
	}

	private static Audience buildAudience(PushLogVo pushLogVo) {
		if (pushLogVo != null) {
			if (pushLogVo.getRegistrationIds() != null && pushLogVo.getRegistrationIds().size() > 0) {
				return Audience.registrationId(pushLogVo.getRegistrationIds());
			} else {
				boolean isBuild = false;
				Builder builder = Audience.newBuilder();
				if (pushLogVo.getAlias() != null && pushLogVo.getAlias().size() > 0) {
					builder.addAudienceTarget(AudienceTarget.alias(pushLogVo.getAlias()));
					isBuild = true;
				}
				if (pushLogVo.getTagValues() != null && pushLogVo.getTagValues().size() > 0) {
					builder.addAudienceTarget(AudienceTarget.tag(pushLogVo.getTagValues()));
					isBuild = true;
				}
				if (pushLogVo.getTagAndValues() != null && pushLogVo.getTagAndValues().size() > 0) {
					builder.addAudienceTarget(AudienceTarget.tag_and(pushLogVo.getTagAndValues()));
					isBuild = true;
				}
				if (isBuild) {
					return builder.build();
				}
			}
		}
		return Audience.all();
	}

	public static void main(String[] args) {
		String masterSecret = "3e7a4a47b74e42f273e60771";
		String appKey = "dd9ccabffb24bc977ad038b8";
		JPushClient jPushClient = new JPushClient(masterSecret, appKey);
		try {
			Map<String, String> map = new HashMap<String, String>();
			PushLogVo pushLogVo = new PushLogVo();
			pushLogVo.setAlert("圈圈测试-大家中秋快乐");
			pushLogVo.setTitle("圈圈测试-大家中秋快乐");
			pushLogVo.setMessage("圈圈测试-大家中秋快乐");
			// android-170976fa8a810211f2d
			// ios-121c83f7602c9059435
			// hy:1517bfd3f7cfbe187cf
			pushLogVo.setStrRegistrationIds("140fe1da9eaa46cdc50");
			pushLogVo.setTimeToLive(9999);
			// map.put("qqtype", "社区");
			// map.put("qqvalue", "Circle");

			pushLogVo.setPushBizz(PushBizzEnum.APP_UPGRADE_NATIVE.getCode());
			pushLogVo.setTimeToLive(86400);
			pushLogVo.setType(PushTypeEnum.MESSAGE.getCode());
			map.put(PushConst.MAPKEY_PUSH_MAPEXTRAS_PUSHBIZZ, "" + PushBizzEnum.APP_UPGRADE_NATIVE.getCode());
			List<PubUpgradeContent> contentList = new ArrayList<PubUpgradeContent>(10);
			String content = "修复圈子;改善流程;跳转参数";
			if (StringUtil.isNotBlank(content)) {
				String[] contentArr = content.split(";");
				if (contentArr != null && contentArr.length > 0) {
					int index = 1;
					for (String string : contentArr) {
						if (StringUtil.isBlank(string)) {
							continue;
						}
						PubUpgradeContent c = new PubUpgradeContent();
						c.setId(index);
						c.setContent(string.trim());
						contentList.add(c);
						index++;
					}
				}
			}

			map.put(PushConst.MAPKEY_PUSH_MAPEXTRAS_PUSHPUBUPGRADE_CONTENTLIST, JsonUtil.toJson(contentList));
			map.put(PushConst.MAPKEY_PUSH_MAPEXTRAS_PUSHPUBUPGRADE_NATIVEVERSION, "3.1.3");
			map.put(PushConst.MAPKEY_PUSH_MAPEXTRAS_PUSHPUBUPGRADE_WXZADDRESSANDROID,
					PubConst.DEFAULTVALUE_PUB_UPGRADE_WXZ);
			map.put(PushConst.MAPKEY_PUSH_MAPEXTRAS_PUSHPUBUPGRADE_WXZADDRESSIOS,
					PubConst.DEFAULTVALUE_PUB_UPGRADE_WXZ);

			pushLogVo.setMapExtras(map);
			jPushClient.sendPush(buildAlertNMessage(pushLogVo));
		} catch (APIConnectionException | APIRequestException e) {
			e.printStackTrace();
		}
	}
}
